/**
 * 文字trigger
 *
 * Created by GUY on 2015/9/15.
 * @class BI.TextTrigger
 * @extends BI.Trigger
 */
BI.TextTrigger = BI.inherit(BI.Trigger, {

    props: function () {
        var self = this;
        return {
            baseCls: "bi-text-trigger",
            height: 24,
            textHgap: 6,
            textCls: "",
            allowClear: false,
            title: function () {
                return self.text.getText();
            },
            defaultText: "",
            text: "",
        };
    },

    render: function () {
        var self = this, o = this.options, c = this._const;

        var text = this.getText();

        var defaultText = this.getDefaultText();

        var label = {
            type: "bi.label",
            ref: function (_ref) {
                self.text = _ref;
            },
            cls: `select-text-label ${o.textCls} ${!BI.isNotEmptyString(text) && BI.isNotEmptyString(defaultText) ? "bi-tips" : ""}`,
            textAlign: "left",
            height: o.height,
            text: text || o.defaultText,
            tipType: o.tipType,
            warningTitle: o.warningTitle,
            hgap: o.textHgap,
            vgap: o.textVgap,
            lgap: o.textLgap,
            rgap: o.textRgap,
            tgap: o.textTgap,
            bgap: o.textBgap,
            readonly: o.readonly
        };

        var triggerButton = {
            type: "bi.trigger_icon_button",
            ref: function (_ref) {
                self.triggerButton = _ref;
            },
            width: o.triggerWidth || o.height
        };

        return ({
            type: "bi.horizontal_fill",
            columnSize: ["fill", o.triggerWidth || o.height],
            items: [
                {
                    el: label,
                    width: "fill"
                }, {
                    el: o.allowClear ? {
                        type: "bi.vertical_adapt",
                        horizontalAlign: "left",
                        scrollable: false,
                        items: [
                            {
                                el: {
                                    type: "bi.icon_button",
                                    ref: function (_ref) {
                                        self.clearBtn = _ref;
                                    },
                                    cls: "close-h-font " + (o.allowClear ? "clear-button" : ""),
                                    stopPropagation: true,
                                    width: o.triggerWidth || o.height,
                                    height: o.height,
                                    invisible: !BI.isNotEmptyString(o.text),
                                    handler: function () {
                                        self.fireEvent(BI.TextTrigger.EVENT_CLEAR);
                                    },
                                },
                            }, {
                                el: triggerButton,
                            }
                        ]
                    } : triggerButton,
                }
            ]
        });
    },

    getText: function () {
        var o = this.options;
        return BI.isFunction(o.text) ? o.text() : o.text;
    },

    getDefaultText: function () {
        var o = this.options;
        return BI.isFunction(o.defaultText) ? o.defaultText() : o.defaultText;
    },

    getTextor: function () {
        return this.text;
    },

    setTextCls: function (cls) {
        var o = this.options;
        var oldCls = o.textCls;
        o.textCls = cls;
        this.text.element.removeClass(oldCls).addClass(cls);
    },

    setText: function (text) {
        if (this.options.allowClear) {
            this.clearBtn.setVisible(BI.isNotEmptyString(text));
        }
        if (BI.isKey(text)) {
            this.text.setText(text);
            this.text.element.removeClass("bi-tips");
        } else if (BI.isKey(this.options.defaultText)) {
            this.text.setText(this.options.defaultText);
            this.text.element.addClass("bi-tips");
        } else {
            this.text.setText("");
            this.text.element.removeClass("bi-tips");
        }
    },

    setTipType: function (v) {
        this.text.options.tipType = v;
        this.options.tipType = v;
    }
});

BI.TextTrigger.EVENT_CLEAR = "EVENT_CLEAR";
BI.shortcut("bi.text_trigger", BI.TextTrigger);
